<template>
    <div>
        <!--面包屑导航区域-->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/buyerIndex' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>购物车</el-breadcrumb-item>
        </el-breadcrumb>

        <!--卡片视图区域-->
        <el-card>
            <!--货物列表区域-->
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="货物名称">
                    <template slot-scope="scope">
                        <el-tag size="medium">{{ scope.row.goodsName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="数量" prop="goodsNum"></el-table-column>
                <el-table-column label="单价" prop="goodsPrice"></el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <el-tag size="medium" type="danger">{{ scope.row.status }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="厂家名称">
                    <template slot-scope="scope">
                        <el-tag size="medium">{{ scope.row.manufacturerName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime" :show-overflow-tooltip='true'>
                    <template slot-scope="scope">
                        {{scope.row.createTime | dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="厂家地址" prop="manufacturerAddress"
                                 :show-overflow-tooltip='true'></el-table-column>
                <el-table-column label="联系方式" prop="manufacturerContact"></el-table-column>
                <el-table-column label="总价" prop="totalPrice"></el-table-column>
                <el-table-column label="操作" fixed="right" width="120px">
                    <template slot-scope="scope">
                        <el-tooltip effect="dark" content="去结算" placement="top" :enterable="false">
                            <el-button type="danger" size="mini" icon="el-icon-shopping-bag-1"
                                       @click="settlement(scope.row.cartId)"></el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                            <el-button type="danger" size="mini" icon="el-icon-delete"
                                       @click="removeCart(scope.row.cartId)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script scoped>
    export default {
        data() {
            return {
                goodsList: [],
                num: '1',
                buyerId: ''
            }
        },
        async created() {
            this.token = window.sessionStorage.getItem("token")
            var buyer = await this.$http.get('/user/buyer/getInformationFromToken', {
                params: {
                    token: this.token
                }
            })
            this.buyerId = buyer.data.data.buyerId
            this.getCart()
        },
        methods: {
            async getCart() {
                var result = await this.$http.get('cart/selectCart', {
                    params: {
                        buyerId: this.buyerId
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error("获取货物列表失败！")
                }
                this.goodsList = result.data.data
            },
            async settlement(cartId){
                //弹框是否删除
                const confirmResult = await this.$confirm('确认结算, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(error => error)
                // 如果用户确认删除，则返回值为字符串 confirm
                // 如果用户取消了删除，则返回值为字符串 cancel
                if (confirmResult !== 'confirm') {
                    return this.$message.info('已取消确认！')
                }
                var result = await this.$http.post('orders/save', {
                        cartId: cartId
                })
                if (result.data.status !== 200) {
                    return this.$message.error(result.data.data)
                }
                this.$message.success("结算成功！")
                this.getCart()
            },
            async removeCart(cartId){
                //弹框是否删除
                const confirmResult = await this.$confirm('此操作将在购物车中删除该货物, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(error => error)
                // 如果用户确认删除，则返回值为字符串 confirm
                // 如果用户取消了删除，则返回值为字符串 cancel
                if (confirmResult !== 'confirm') {
                    return this.$message.info('已取消删除')
                }
                const result = await this.$http.delete('cart/delete', {
                    params: {
                        cartId: cartId
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error('删除货物失败！')
                }
                this.$message.success('删除货物成功！')
                this.getCart()
            }
        }
    }
</script>

<style scoped>
    .el-breadcrumb {
        margin-bottom: 15px;
        font-size: 12px;
    }

    .el-card {
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
    }

    .el-table {
        margin-top: 15px;
        font-size: 12px;
    }

    .el-pagination {
        text-align: right;
        margin-top: 15px;
    }
</style>
